//
//  HMMainViewController.swift
//  Weibo
//
//  Created by Apple on 2017/6/3.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
//  根视图控制器
class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  添加子视图控制器方式1
//        viewControllers = []
        //  添加子视图控制器方式2
//        addChildViewController(vc)
        
        
        let hmTabBar = HMTabBar()
        //  设置代理
        hmTabBar.hmDelegate = self
        //  使用self注意循环引用
        hmTabBar.composeCallBack = { [unowned self] in
            print("我是闭包回调过来的")
        }
        //  设置tabbar
//        self.tabBar = hmTabBar
        //  使用kvc给OC只读属性赋值
        self.setValue(hmTabBar, forKey: "tabBar")
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMMeTableViewController(), title: "我的", imageName: "tabbar_profile")
    }

    //  重载添加子视图控制器方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //  设置tabbaritem的文字和图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //  设置图片的渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title

        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        
        //  合并一句代码
        childController.title = title
        
        //  创建导航控制器
        let nav = HMNavigationController(rootViewController: childController)
        
        //  添加到子视图控制器
        addChildViewController(nav)
        
    }
  

}

//  MARK:   --HMTabBarDelegate
extension HMMainViewController: HMTabBarDelegate {
    func composeOperation() {
        //  
        print("我是代理对象回调过来的")
    }
}
